package com.black.platform.permission.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.black.common.web.ApiResponse;
import com.black.common.web.ApiResponseBuilder;
import com.black.common.web.PageQuery;
import com.black.platform.permission.po.Permission;
import com.black.platform.permission.service.handler.PermissionHandlerService;
import com.black.platform.permission.vo.PermissionTreeVo;
import com.black.common.vo.PermissionVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("permission")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PermissionController {

    private final PermissionHandlerService permissionHandlerService;

    @PostMapping("search")
    public ApiResponse<IPage<Permission>> search(@RequestBody PageQuery<Permission> searchVo) {
        return ApiResponseBuilder.ok(permissionHandlerService.search(searchVo));
    }

    @GetMapping("Id/{id}")
    public ApiResponse<Permission> getById(@PathVariable("id") Long id) {
        return ApiResponseBuilder.ok(permissionHandlerService.getById(id));
    }

    @GetMapping("UIPermission/{systemCode}")
    public ApiResponse<Map<String, List<PermissionTreeVo>>> getUIPermission(@PathVariable("systemCode") String systemCode) {
        return ApiResponseBuilder.ok(permissionHandlerService.getPermissionTree(systemCode));
    }

    @PostMapping("update")
    public ApiResponse<Boolean> update(@RequestBody Permission permission) {
        return ApiResponseBuilder.ok(permissionHandlerService.saveOrUpdate(permission));
    }

    @PostMapping("save")
    public ApiResponse<Boolean> save(@RequestBody Permission permission) {
        return ApiResponseBuilder.ok(permissionHandlerService.saveOrUpdate(permission));
    }

    @DeleteMapping
    public ApiResponse<Boolean> deleteData(@RequestBody List<Long> ids) {
        return ApiResponseBuilder.ok(permissionHandlerService.removeByIds(ids));
    }

    @GetMapping("getAllPermissionsByUser")
    public ApiResponse<Set<String>> getAllPermissionsByUser(@RequestParam("systemCode") String systemCode) {
        return ApiResponseBuilder.ok(permissionHandlerService.getAllPermissionsByUser(systemCode, false)
                .stream().map(PermissionVo::getPermissionCode).collect(Collectors.toSet()));
    }

    @GetMapping("getAllDataPermissionsByUser")
    public ApiResponse<Set<String>> getAllDataPermissionsByUser(@RequestParam("systemCode") String systemCode) {
        return ApiResponseBuilder.ok(permissionHandlerService.getAllDataPermissionsByUser(systemCode)
                .stream().map(PermissionVo::getPermissionCode).collect(Collectors.toSet()));
    }

    @GetMapping("getUserButtonPermissionsByUserId")
    public ApiResponse<List<String>> getUserButtonPermissionsByUserId(@RequestParam("systemCode") String systemCode) {
        return ApiResponseBuilder.ok(permissionHandlerService.getUserButtonPermissionsByUserId(systemCode, false));
    }

    @GetMapping("getAllRouterPermissions")
    public ApiResponse<Map<String, Set<Long>>> getAllRouterPermissions() {
        return ApiResponseBuilder.ok(permissionHandlerService.getAllRouterPermissions());
    }

    @GetMapping("getAllWhitePath")
    public ApiResponse<Set<String>> getAllWhitePath() {
        return ApiResponseBuilder.ok(permissionHandlerService.getAllWhitePath());
    }
}
